package sw.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import sw.entity.College;
import sw.repo.CollegeRepo;

@Controller //控制
public class CollegeController {
	@Autowired   //引入数据库接口
	private CollegeRepo collegeRepository;

	
	@GetMapping("/college") //显示学院列表视图
	public String showCollegePage(Model model){
		Iterable<College> colleges = collegeRepository.findAll();
		model.addAttribute("colleges",colleges);
		return "college/index";
	}
	
	@GetMapping("/college/form")
	public String showCollegeAddPage(){
		return "college/form";
	}
	
	@PostMapping("/college/college") //接受来自前端的数据
	public String showForm(College college){
		collegeRepository.save(college);  //保存到数据库
		return "redirect:/college"; //重新定向到学院视图
	}
	
}




